/**
 ============================================================================
 Author     : Jonathan MERCIER
 Version    : 0.1
 License    : GPL v3+
 Copyright  : (c)Jonathan MERCIER (read doc for more information)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 Description :
 ============================================================================
 */

import tango.io.Stdout          : Stdout, Stderr;
import tango.text.Arguments     : Arguments;
import tango.io.FilePath        : FilePath;
import tango.stdc.stdlib        : exit, EXIT_FAILURE, EXIT_SUCCESS;
import tango.text.locale.Core   : Culture;
import Controller;

bool verbose    = false;

void printDebug(char[] message) {
    if (verbose)
        Stdout(message).newline;
}

void main(char [][] args){
    //Culture culture     = new Culture("");
    FilePath  inputPath;
    char[]  outputPath;
    char[]  helpText    =   "Available options:\n
                            \t\t-h\t--help\t\tthis help\n
                            \t\t-v\t--verbose\tenable verbose mode\n
                            \t\t-i\t--input\t\tinput file\n
                            \t\t-o\t--output\toutput file\n";
    //Parse command line option defaut prefix '-' and '--' is used
    Arguments getopt = new Arguments();
    //help option
    getopt("help").params(0).aliased('h').halt;
    //verbose option
    getopt("verbose").params(0).aliased('v');
    //Input file option
    getopt("input").required.params(1).aliased('i');
    //Output file option
    getopt("output").params(1).aliased('o');
    //Parsing command line, if they are no option or if they are not a required option
    if (getopt.parse( args[1..$] ) == false ) {
        //Check if help option is present
        if (getopt("help").set){
            Stdout(helpText);
        }
        else {
            Stderr(getopt.errors(&Stderr.layout.sprint)).flush;
            Stderr(helpText).flush;
            exit(EXIT_FAILURE);
        }
    }
    //Check if verbose option is present
    if (getopt("verbose").set){
       verbose = true;
       printDebug("Verbose mode activated");
    }
    //Check if input option is present
    if (getopt("input").set){
        inputPath = new FilePath(getopt("input").assigned[0]);
        printDebug(inputPath.toString);
        if (! inputPath.exists){
            Stderr("Error: Path do not exist!").newline.flush;
            exit(EXIT_FAILURE);
        }
        else if (! inputPath.isFile){
            Stderr("Error: is not a file!").newline.flush;
            exit(EXIT_FAILURE);
        }
    }
    //Check if output option is present
    if (getopt("output").set){
        outputPath = getopt("output").assigned[0];
        printDebug(outputPath);
    }
    else{
        outputPath          = inputPath.path ~ inputPath.name ~ ".out";
        FilePath outPutName = new FilePath(outputPath);
        if (outPutName.exists){
            Stderr("Error: "~outputPath~" exist! rename this file and re-try").newline.flush;
            exit(EXIT_FAILURE);
        }
        outPutName.createFile;
        printDebug(outputPath);
    }
    auto controller = new Controller(inputPath.toString, outputPath);
    controller.start;
}
